import Apis from "@/apis"
import { createLogger } from "@/utils/logger"
import { toast } from "react-toastify"
const logger = createLogger("ApiFetchers")

interface UseDepositSubmitProps {
    onSuccess?: () => void
    onError?: (error: Error) => void
}

export default ({ onSuccess, onError }: UseDepositSubmitProps = {}) => {
    const handleWebDeposit = async (params: {
        amount: number
        pid: string
        channelName: string
    }) => {
        try {
            const redirectUrlPayload: WalletType.DepositConfirmPageParams = {
                channel: params.channelName,
                amount: params.amount.toString(),
                urlBeforeRedirect: window.location.href,
                pathBeforeRedirect: location.pathname,
                queryBeforeRedirect: location.search,
            }

            // const redirectUrl = getRedirectUrlToDepositConfirmPage(redirectUrlPayload)
            // const response = await ApiFetchers.rechargeByWeb({
            //     amount: params.amount.toString(),
            //     channelName: params.channelName,
            //     redirectUrl: redirectUrl.toString(),
            // })

            const response = await Apis.v1.refUpdateFinancePaymentDeposit({
                amount: params.amount.toString(),
                pid: params.pid,
                device: "web",
            })
            logger.debug("DEPOSIT", response)
            if (response.data.data) {
                onSuccess?.()
                const { order_id, pay_url: redirectUrlToPayment } = response.data.data
                if (redirectUrlToPayment) {
                    sessionStorage.setItem("last_deposit_order_id", order_id ?? "")
                    window.location.href = redirectUrlToPayment
                }
            } else {
                toast(response.data as string)
                throw new Error("Recharge failed")
            }
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error)
            toast.error(errorMessage)
            onError?.(error instanceof Error ? error : new Error(errorMessage))
        }
    }

    const handleLazadaDeposit = async (params: { amount: number; channelName: string }) => {
        try {
            // Lazada 充值逻辑
            // const response = await ApiFetchers.rechargeByExchange({
            //     amount: params.amount.toString(),
            //     channelName: params.channelName,
            // })
            // if (response) {
            //     onSuccess?.()
            //     if (response.redirectUrl) {
            //         window.location.href = response.redirectUrl
            //     }
            // } else {
            //     throw new Error("Recharge failed")
            // }
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error)
            toast.error(errorMessage)
            onError?.(error instanceof Error ? error : new Error(errorMessage))
        }
    }

    return {
        handleWebDeposit,
        handleLazadaDeposit,
    }
}
